package com.northpool.operator.common.algorithm.annotation;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Set;

@SupportedAnnotationTypes("com.northpool.operator.common.algorithm.annotation.PluginInfo")
public class AnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for (javax.lang.model.element.Element element : roundEnv.getElementsAnnotatedWith(PluginInfo.class)) {
            TypeElement typeElement = (TypeElement) element;
            PluginInfo pluginInfo = typeElement.getAnnotation(PluginInfo.class);

            try (FileWriter writer = new FileWriter("output/" + typeElement.getSimpleName() + ".xml")) {
                writer.write("<plugin>\n");
                writer.write("  <name>" + pluginInfo.name() + "</name>\n");
                writer.write("  <version>" + pluginInfo.version() + "</version>\n");
                writer.write("  <description>" + pluginInfo.description() + "</description>\n");
                writer.write("  <maxtim>" + pluginInfo.maxtime() + "</maxtim>\n");
                writer.write("  <CPU>" + pluginInfo.CPU() + "</CPU>\n");
                writer.write("  <GPU>" + pluginInfo.GPU() + "</GPU>\n");
                writer.write("  <disk>" + pluginInfo.disk() + "</disk>\n");
                writer.write("  <memory>" + pluginInfo.memory() + "</memory>\n");
                writer.write("  <run>" + pluginInfo.run() + "</run>\n");
                writer.write("</plugin>\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}